package com.ruoyi.app.zzq.controller;

import com.ruoyi.app.zzq.domain.User;
import com.ruoyi.app.zzq.service.InforService;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/infor")
public class InforController extends BaseController {
    @Autowired
    private InforService inforService;


    @ApiOperation("查看个人信息功能")
    @GetMapping("/selectuserinfo")
    public AjaxResult SelectUserById()
    {
        Long userId = SecurityUtils.getLoginUser().getUserId();
        return AjaxResult.success(inforService.SelectUserById(""+userId));
    }

    @ApiOperation("修改个人信息功能(包括头像)")
    @PutMapping("/updateuserbyid")
    public AjaxResult UpdateUserById(@RequestBody User user)
    {
        Long userId = SecurityUtils.getLoginUser().getUserId();
        user.setUserId(userId+"");
//        System.out.println(user);
        return AjaxResult.success(inforService.UpdateUserById(user));
    }

    @ApiOperation("修改个人密码功能")
    @GetMapping("/updatepassword")
    public AjaxResult UpdatePassWord(String idNum,String oldPassword,String newPassword)
    {
        return AjaxResult.success(inforService.UpdatePassWord(idNum,oldPassword,newPassword));
    }
    /**
     * 头像上传
     */
    @Log(title = "用户头像", businessType = BusinessType.UPDATE)
    @PostMapping("/avatar")
    public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file) throws IOException
    {
        if (!file.isEmpty())
        {
            Long userId = SecurityUtils.getLoginUser().getUserId();
            String url = inforService.upload(file,userId+"");
            //            修改头像路径
            User user=new User();
            user.setUserId(userId+"");
            user.setAvatar(url);
            inforService.UpdateUserById(user);
            return AjaxResult.success(url);
        }
        return AjaxResult.error("上传图片异常，请联系管理员");
    }

}
